<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>智慧园区-登录</h1>
      <el-card shadow="never" class="login-card">
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
          <div class="label">账号</div>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" />
          </el-form-item>
          <div class="label">密码</div>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" :show-password="showPassword" />
            <!-- <i><svg-icon icon-class="eye" /></i> -->
          </el-form-item>
          <div class="label">模型uuid</div>
          <el-form-item prop="randomUID">
            <el-input v-model="loginForm.randomUID" :show-password="showUID" />
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button style="width:350px" type="primary" @click.native="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>

</template>

<script>
import { FORMDATA_KEY } from '@/constants/KEY'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        // username: process.env.NODE_ENV === 'development' ? 'demo' : '',
        // password: process.env.NODE_ENV === 'development' ? 'Hmzs%001' : '',
        // randomUID: process.env.NODE_ENV === 'development' ? '66ea8d2a-7416-4be5-ba7e-c030cf88b3f3' : '',
        username: '',
        password: '',
        randomUID: '',
        isAgree: process.env.NODE_ENV === 'development'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        randomUID: [
          { required: true, message: '请输入任意生成的模型id!', trigger: 'blur' }
        ]
      },
      showPassword: false,
      showUID: false

    }
  },
  created() {
    if (localStorage.getItem(FORMDATA_KEY)) {
      this.loginForm = JSON.parse(localStorage.getItem(FORMDATA_KEY))
    }
  },
  methods: {
    async login() {
      // 1整体校验
      await this.$refs.loginRef.validate()
      if (this.loginForm.isAgree) {
        localStorage.setItem(FORMDATA_KEY, JSON.stringify(this.loginForm))
      }
      // 派发异步任务
      this.$store.dispatch('user/login', this.loginForm)
    }
  }

}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  align-items: stretch;
  height: 100%;

  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png) no-repeat center / cover;
    border-top-right-radius: 60px;

    .logo {
      flex: 3;
      background: rgba(38, 72, 176) url(../../assets/common/login_back.png) no-repeat center / cover;
      border-top-right-radius: 60px;
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      justify-content: center;
      padding: 0 100px;
    }
  }

  .form {
    flex: 2;
    justify-content: center;
    padding-left: 176px;
    padding-top: 3vw;

    .el-card {
      border: none;
      padding: 0;

      .label {
        margin-bottom: 8px;
      }
    }

    h1 {
      padding-left: 20px;
      font-size: 24px;
    }

    ::v-deep .el-input {
      display: block;
      width: 350px;
      height: 44px;

      .el-input__inner {
        background: #f4f5fb;
      }
    }

    ::v-deep .el-checkbox {
      color: #606266;
    }

    /* 新增样式，让 el-form-item 独占一行 */
    ::v-deep .el-form-item {
      display: block !important;
      /* 增加间距，使布局更美观 */
    }

  }
}
</style>
